<?php session_start(); 
include ("../function.php");
include "../dbconnect.php";
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>IQ TEST</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="<?php echo $base_url;?>css/bootstrap.css" media="screen">
    <link rel="stylesheet" href="<?php echo $base_url;?>css/bootswatch.min.css">
	<link rel="stylesheet" href="<?php echo $base_url;?>css/form_validation.css">
    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="<?php echo $base_url;?>js/html5shiv.js"></script>
      <script src="<?php echo $base_url;?>js/respond.min.js"></script>
    <![endif]-->
	<script src="<?php echo $base_url;?>js/jquery-1.10.2.min.js"></script>
	<script src="<?php echo $base_url;?>js/bootstrap.min.js"></script>
    <script src="<?php echo $base_url;?>js/bootswatch.js"></script>
  </head>
  <body>
<?php include ("../menu.php"); ?>
    <div class="container">  
      <!-- Forms
      ================================================== -->
      <div>
        <div class="row">
          <div class="col-lg-12">
            <div class="page-header">
              <h2 id="forms">ผลการทดสอบ </h2>
            </div>
          </div>
        </div>
        <div class="row">
            <div class="well bs-component">
            <?php 
            $id_excercise = (isset($_POST['id_excercise']))?$_POST['id_excercise']:0;
			$times = (isset($_POST['times']))?$_POST['times']:1;
            //$student_code = $_SESSION['student_code'];
            $student_id = (isset($_POST['student_id']))?$_POST['student_id']:$_SESSION['student_id'];
            
            if($id_excercise > 0)
            {
				$numrows = 0;
            	//query question 
            	$sql = "SELECT * FROM question WHERE id_excercise=".$id_excercise." AND status=1";
            	$result = mysql_query($sql, $link);
            	mysql_query("SET NAMES UTF8");
				
				if($result)$numrows = mysql_num_rows($result);			
            	
            	if($numrows > 0)
            	{
            		$total_score =0;
            		
            		//get times to do <<<<<<<<<<<<<<<<<<<<<<<<
            		//$times = check_times_by_student_id($id_excercise,$student_id);
            		//$new_times = ($times < 2)?$times+1:1;
            		//echo $new_times."<new  old>".$times;
            		//get full score <<<<<<<<<<<<<<<<<<<<<<<<
            		$full_score = get_full_total($id_excercise);
            		
            		//insert record
            		$sqlInsert = "INSERT INTO student_excercise(id_student,id_excercise,total_score,full_score,created_date,times)
    						  VALUES('".$student_id."','".$id_excercise."','0','".$full_score."','".date('Y-m-d')."','".$times."')";
            		mysql_query($sqlInsert, $link);
            		$student_excercise_id = mysql_insert_id();

            		while ($arr = mysql_fetch_array($result))
            		{
            			$id_question = $arr['id_question'];
            			
            			//get post data
            			$input_name = 'question'.$id_question;
            			$val_input_name = (isset($_POST[$input_name]))? $_POST[$input_name]:0;
            			
            			//set  strtolower value
            			$val_input_name = array_map('strtolower', $val_input_name);
            			
            			//trim
            			$val_input_name = array_map('trim',$val_input_name);
            			
            			//get ans
            			$arr_ans = get_ans_by_id_question($id_question);
            			
            			//check score
            			if(arrays_are_same($val_input_name, $arr_ans) === TRUE)
            			{
            				//if right +1
            				$total_score +=$arr['score'];
            			}
            			
            			/*echo "<pre>";
            			print_r($_POST[$input_name]);
            			echo "</pre><br>";*/
            		}
            		
            		//update score to DB
            		$sql = "UPDATE student_excercise SET total_score = '".$total_score."' WHERE id_student_excercise =".$student_excercise_id;
            		$update = mysql_query($sql, $link);
            		if(!$update)
            		{
            			echo "เกิดข้อผิดพลาด ไม่สามารถตรวจแบบทดสอบได้ กรุณาทำอีกครั้ง";
            		}
            		else 
            		{

            			//get name excercise <<<<<<<<<<<<<<<<<<<<<<<<
            			$ex_info = get_info_excercise($id_excercise);
            			
            			//get result <<<<<<<<<<<<<<<<<<<<<<<<
            			$result_detail = get_result_detail($total_score,$ex_info['id_group_rules']);
            			
            			echo "<h4> แบบทดสอบ : ".$ex_info['title']." คุณได้คะแนน  ".$total_score."/".$full_score." ระดับ iQ ของคุณคือ : ".$result_detail.'</h4><br>';
            		}
            	}
            }
            
            function get_ans_by_id_question($id_question = 0)
            {
            	include "../dbconnect.php";
            	$ans = array();
            	if($id_question > 0)
            	{
            		$sql = "SELECT title FROM answer WHERE id_question=".$id_question;
            		$result = mysql_query($sql, $link);
            		mysql_query("SET NAMES UTF8");
            		$numrows = mysql_num_rows($result);
            		if($numrows > 0)
            		{
            			$i=0;
            			while ($arr = mysql_fetch_array($result))
            			{
            				$ans[$i] = $arr['title'];
            				$i++;
            			}
            		}
            	}
            	return $ans;
            }
            
            function arrays_are_same($array1, $array2) {
            	sort($array1);
            	sort($array2);
            	return $array1==$array2;
            }
            
            ?>
           
            </div>
        </div>
      </div>
      <footer>
        <div class="row">
          <div class="col-lg-12">
            <ul class="list-unstyled">
              <li class="pull-right"><a href="#top">Back to top</a></li>
            </ul>
          </div>
        </div>
      </footer>
    </div>
  </body>
</html>
